
<template>
  <div class="content-right-box">
    <div class="top-card">
      <div class="card1">
        <div class="com-title">
          <div class="l-title">当前库存量统计</div>
        </div>
        <div class="echart-box1">
          <div class="echart" ref="echartsRight1"></div>
        </div>
      </div>
      <div class="card2">
        <div class="com-title">
          <div class="l-title">储量占比分析</div>
        </div>
        <div class="echart-box2">
          <div class="echart" ref="echartsRight2"></div>
        </div>
      </div>
    </div>

    <div class="bottom-card">
      <div class="com-title">
        <div class="l-title">日收发油趋势</div>
        <div class="r-date">
          <el-date-picker
            v-model="dateValue"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            :clearable="false"
            range-separator="~"
          ></el-date-picker>
        </div>
      </div>
      <div class="echart-box3">
        <div class="echart" ref="echartsRight3"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      listData: [{}, {}, {}, {}, {}, {}],
      echart1: null,
      echart2: null,
      echart3: null,
      dateValue: []
    }
  },
  created () {
    const endDate = this.$dayjs(new Date()).format('YYYY-MM-DD')
    const startDate = this.$dayjs().subtract(6, 'day').format('YYYY-MM-DD')
    this.dateValue = [startDate, endDate]
    console.log(' this.dateValue : ', this.dateValue)
  },
  mounted () {
    this.getEchart1()
    this.getEchart2()
    this.getEchart3()
    window.addEventListener('resize', () => {
      this.changeResize()
    })
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.changeResize)
    this.echart1 && this.echart1.dispose()
    this.echart2 && this.echart2.dispose()
    this.echart3 && this.echart3.dispose()
  },
  methods: {
    changeResize () {
      this.echart1 && this.echart1.resize()
      this.echart2 && this.echart2.resize()
      this.echart3 && this.echart3.resize()
    },
    getEchart1 () {
      this.$nextTick(() => {
        const dataY = ['大豆油', '花生油', '橄榄油', '菜籽油', '花生油2', '花生油3', '花生油4']
        // let dataY = datas.map(item => {
        //   if (item.split('').length > 10) {
        //     return item.substring(0, 6) + '...'
        //   } else {
        //     return item
        //   }
        // })
        // dataY = dataY.join('')
        const values = [100, 200, 300, 200, 300, 300, 200]
        const maxList = [500, 500, 500, 500, 500, 500, 500]
        this.echart1 = this.$echarts.init(this.$refs.echartsRight1)
        const CubeLeft = this.$echarts.graphic.extendShape({
          shape: {
            x: 0,
            y: 0
          },
          buildPath: function (ctx, shape) {
            const yAxisPoint = shape.yAxisPoint
            const c0 = [shape.x, shape.y]
            const c1 = [shape.x + 5, shape.y - 5]
            const c2 = [yAxisPoint[0] + 5, yAxisPoint[1] - 5]
            const c3 = [yAxisPoint[0], yAxisPoint[1]]
            ctx
              .moveTo(c0[0], c0[1])
              .lineTo(c1[0], c1[1])
              .lineTo(c2[0], c2[1])
              .lineTo(c3[0], c3[1])
              .closePath()
          }
        })
        const CubeRight = this.$echarts.graphic.extendShape({
          shape: {
            x: 0,
            y: 0
          },
          buildPath: function (ctx, shape) {
            const yAxisPoint = shape.yAxisPoint
            const c1 = [shape.x, shape.y]
            const c2 = [yAxisPoint[0], yAxisPoint[1]]
            const c3 = [yAxisPoint[0] + 5, yAxisPoint[1] + 10]
            const c4 = [shape.x + 5, shape.y + 10]
            ctx
              .moveTo(c1[0], c1[1])
              .lineTo(c2[0], c2[1])
              .lineTo(c3[0], c3[1])
              .lineTo(c4[0], c4[1])
              .closePath()
          }
        })
        const CubeTop = this.$echarts.graphic.extendShape({
          shape: {
            x: 0,
            y: 0
          },
          buildPath: function (ctx, shape) {
            const c1 = [shape.x, shape.y]
            const c2 = [shape.x + 5, shape.y + 10]
            const c3 = [shape.x + 10, shape.y + 5]
            const c4 = [shape.x + 5, shape.y - 5]
            ctx
              .moveTo(c1[0], c1[1])
              .lineTo(c2[0], c2[1])
              .lineTo(c3[0], c3[1])
              .lineTo(c4[0], c4[1])
              .closePath()
          }
        })
        this.$echarts.graphic.registerShape('CubeLeft', CubeLeft)
        this.$echarts.graphic.registerShape('CubeRight', CubeRight)
        this.$echarts.graphic.registerShape('CubeTop', CubeTop)
        const options = {
          grid: {
            left: 2,
            right: '2%',
            bottom: 10,
            top: 2,
            containLabel: true
          },
          yAxis: {
            type: 'category',
            data: dataY,
            axisLine: {
              show: false,
              lineStyle: {
                color: 'white'
              }
            },
            axisTick: {
              show: false,
              length: 9,
              alignWithLabel: true,
              lineStyle: {
                color: '#7DFFFD'
              }
            },
            axisLabel: {
              fontSize: 12
            }

          },
          xAxis: {
            type: 'value',
            axisLine: {
              show: false,
              lineStyle: {
                color: 'white'
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false,
              fontSize: 12
            }
            // boundaryGap: ['10%', '1%']
          },
          series: [
            {
              type: 'custom',
              renderItem: function (params, api) {
                const location = api.coord([api.value(0), api.value(1)])
                return {
                  type: 'group',
                  children: [
                    {
                      type: 'CubeLeft',
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        yAxisPoint: api.coord([0, api.value(1)])
                      },
                      style: {
                        fill: 'rgba(7,29,97,.6)'
                      }
                    },
                    {
                      type: 'CubeRight',
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        yAxisPoint: api.coord([0, api.value(1)])
                      },
                      style: {
                        fill: 'rgba(10,35,108,.7)'
                      }
                    },
                    {
                      type: 'CubeTop',
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        yAxisPoint: api.coord([0, api.value(1)])
                      },
                      style: {
                        fill: 'rgba(11,42,106,.8)'
                      }
                    }
                  ]
                }
              },
              data: maxList
            },
            {
              type: 'custom',
              renderItem: (params, api) => {
                const location = api.coord([api.value(0), api.value(1)])
                return {
                  type: 'group',
                  children: [
                    {
                      type: 'CubeLeft',
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        yAxisPoint: api.coord([0, api.value(1)])
                      },
                      style: {
                        fill: new this.$echarts.graphic.LinearGradient(1, 0, 0, 0, [
                          {
                            offset: 0,
                            color: '#3B80E2'
                          },
                          {
                            offset: 1,
                            color: '#49BEE5'
                          }
                        ])
                      }
                    },
                    {
                      type: 'CubeRight',
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        yAxisPoint: api.coord([0, api.value(1)])
                      },
                      style: {
                        fill: new this.$echarts.graphic.LinearGradient(1, 0, 0, 0, [
                          {
                            offset: 0,
                            color: '#3B80E2'
                          },
                          {
                            offset: 1,
                            color: '#49BEE5'
                          }
                        ])
                      }
                    },
                    {
                      type: 'CubeTop',
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        yAxisPoint: api.coord([0, api.value(1)])
                      },
                      style: {
                        fill: new this.$echarts.graphic.LinearGradient(1, 0, 0, 0, [
                          {
                            offset: 0,
                            color: '#3B80E2'
                          },
                          {
                            offset: 1,
                            color: '#49BEE5'
                          }
                        ])
                      }
                    }
                  ]
                }
              },
              data: values
            },
            {
              type: 'bar',
              label: {
                normal: {
                  show: true,
                  position: 'right',
                  formatter: e => {
                    return values[e.dataIndex]
                  },
                  fontSize: 16,
                  color: '#fff',
                  offset: [-30, 4]
                }
              },
              itemStyle: {
                color: 'transparent'
              },
              data: maxList
            }
          ]
        }

        this.echart1.clear()
        this.echart1.setOption(options)
      })
    },
    getEchart2 () {
      this.$nextTick(() => {
        this.echart2 = this.$echarts.init(this.$refs.echartsRight2)

        const options = {
          legend: {
            top: 'center',
            right: '10%',
            orient: 'vertical',
            textStyle: {
              color: '#fff',
              fontSize: 14
            },
            itemHeight: 8,
            itemWidth: 8,
            itemGap: 16,
            borderRadius: '50%',
            formatter: (name) => {
              // 假设你有一个 echarts 实例的引用
              const chart = this.$echarts.getInstanceByDom(this.$refs.echartsRight2)
              // 获取系列（series）中相关数据
              let total = 0
              for (let i = 0; i < chart.getOption().series.length; i++) {
                for (let j = 0; j < chart.getOption().series[i].data.length; j++) {
                  total += chart.getOption().series[i].data[j].value
                }
              }
              let percentage = 0
              for (let i = 0; i < chart.getOption().series.length; i++) {
                for (let j = 0; j < chart.getOption().series[i].data.length; j++) {
                  if (chart.getOption().series[i].data[j].name === name) {
                    percentage = (chart.getOption().series[i].data[j].value / total * 100).toFixed(2)
                    return `${name}：${chart.getOption().series[i].data[j].value}吨 (${percentage}%)`
                  }
                }
              }
              return name
            }

          },
          toolbox: {
            show: false
          },

          tooltip: {
            trigger: 'item',
            formatter: '{b}：{c}吨 ({d}%)',
            backgroundColor: '#0082CF70',
            borderColor: '#2880FF',
            borderWidth: 1,
            textStyle: {
              color: '#fff',
              fontSize: 14
            }
          },
          series: [
            {
              name: '储量占比分析',
              type: 'pie',
              radius: ['60%', '90%'],
              center: ['30%', '50%'],
              roseType: 'area',
              label: { show: false },
              labelLine: { show: false },
              selectedOffset: 30,
              // selectedMode: true,
              itemStyle: {
                // borderRadius: 8
              },
              data: [
                { value: 140, name: '花生油' },
                { value: 380, name: '大豆油' },
                { value: 532, name: '橄榄油' },
                { value: 700, name: '花生油2' }

              ]
            }
          ]
        }
        this.echart2.clear()
        this.echart2.setOption(options)
      })
    },
    getEchart3 () {
      this.$nextTick(() => {
        this.echart3 = this.$echarts.init(this.$refs.echartsRight3)

        const options = {
          title: {
            show: false
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            },
            backgroundColor: '#0082CF70',
            borderColor: '#2880FF',
            borderWidth: 1,
            textStyle: {
              color: '#fff',
              fontSize: 14
            }
          },
          legend: {
            data: ['大豆油', '橄榄油', '花生油', '猪油'],
            right: 10,
            top: 2,
            textStyle: {
              color: '#fff',
              fontSize: 14
            },
            itemWidth: 10,
            itemHeight: 10,
            type: 'scroll',
            orient: 'horizontal',
            itemGap: 14
          },
          grid: {
            left: '2%',
            right: '2%',
            bottom: '2%',
            top: '16%',
            containLabel: true
          },

          xAxis: {
            type: 'category',
            data: ['2024-07-01', '2024-07-02', '2024-07-03', '2024-07-04', '2024-07-05', '2024-07-06', '2024-07-07'],
            axisLine: {
              lineStyle: {
                color: '#0A476E'

              }
            },
            axisLabel: {
              textStyle: {
                color: '#fff',
                fontSize: 14
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          },
          yAxis: {
            type: 'value',
            name: '吨',
            nameTextStyle: {
              color: '#ffffff70',
              fontSize: 14,
              verticalAlign: 'middle',
              align: 'center'
            },

            axisLine: {
              show: true,
              lineStyle: {
                color: '#0A476E'
              }
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              textStyle: {
                color: '#fff',
                fontSize: 14
              }
            }
          },
          series: [
            {
              name: '大豆油',
              type: 'line',
              stack: 'Total',
              data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
              name: '橄榄油',
              type: 'line',
              stack: 'Total',
              data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
              name: '花生油',
              type: 'line',
              stack: 'Total',
              data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
              name: '猪油',
              type: 'line',
              stack: 'Total',
              data: [320, 332, 301, 334, 390, 330, 320]
            }

          ]
        }
        this.echart3.clear()
        this.echart3.setOption(options)
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.content-right-box {
  width: 100%;
  height: 100%;
  .top-card {
    margin: 0 14px 0 0;
    height: 58%;
    background-color: #092b4f;
    .card1 {
      .echart-box1 {
        margin: 16px;
        height: 200px;
        .echart {
          width: 100%;
          height: 100%;
        }
      }
    }
    .card2 {
      margin-top: 16px;
      .echart-box2 {
        margin: 16px;
        height: 200px;
        .echart {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
  .bottom-card {
    margin: 14px 14px 0;
    height: calc(42% - 14px);
    background-color: #092b4f;
    .echart-box3 {
      margin: 16px;
      height: 300px;
      .echart {
        width: 100%;
        height: 100%;
      }
    }
  }
}

.com-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  background-image: url("../assets/img/title-bg.png");
  background-size: auto 100%;
  background-repeat: no-repeat;
  padding: 0 20px 0 50px;
  .l-title {
    font-size: 18px;
    letter-spacing: 2px;
    font-family: Alimama;
    background-image: -webkit-linear-gradient(bottom, #14a8ed, #fff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
  .r-date {
    ::v-deep .el-date-editor.el-range-editor {
      border: 1px solid #04577e;
      height: 38px;
      background-color: transparent;
      padding-right: 0px;

      border-radius: 4px;
      width: 266px;
      .el-input__inner,
      .el-range-input {
        background-color: transparent;
        border: none;
        text-align: center;
        color: #63bdf1;
        font-size: 16px;
        display: flex;
        align-items: center;
        line-height: 38px;
        justify-content: center;
        height: 100%;
        padding-right: 2px;
      }
      .el-input__prefix,
      .el-input__icon {
        color: #63bdf1;
        transform: translateY(-1px);
        font-size:14px;

      }
      .el-range-separator {
        color: #63bdf1;
        transform: translateX(-4px);
      }
      .el-range__close-icon {
        display: none;
      }
    }
  }
}
</style>
